package com.hzqiuxm.securitydemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * Copyright © 2018年 ziniuxiaozhu. All rights reserved.
 *
 * @Author 临江仙 hzqiuxm@163.com
 * 安全配置类
 * @Date 2018/8/31 16:04
 */
@Configuration
@EnableWebSecurity
//@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    @Override
    protected void configure(HttpSecurity http) throws Exception {


        http.headers().contentTypeOptions().disable();
        http.csrf().disable();

        http.authorizeRequests().antMatchers("/guest").hasRole("GUEST");
        http.authorizeRequests().antMatchers("/user").hasRole("USER");
        http.authorizeRequests().antMatchers("/admin").hasRole("ADMIN");

        http
                .authorizeRequests()
                .antMatchers("*/", "/login", "/*", "/sys/login", "/sys/logout").permitAll()//1根路径和/login路径不拦截
                .anyRequest().authenticated()
                .and()
                .formLogin();

    }

    @Override
    /**
     * 认证授权类配置：
     * 配置用户，密码，角色
     */
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {


        auth
                .inMemoryAuthentication()
                .withUser("qiuxm").password("qiuxm").roles("USER")
                .and()
                .withUser("zhangshan").password("zhangshan").accountLocked(true).roles("USER")
                .and()
                .withUser("lisi").password("lisi").accountExpired(true).roles("USER")
                .and()
                .withUser("hzqiuxm").password("hzqiuxm").roles("USER", "ADMIN");

        auth.inMemoryAuthentication().withUser("guest").password("123").roles("GUEST");
        auth.inMemoryAuthentication().withUser("user").password("123").roles("USER");
        auth.inMemoryAuthentication().withUser("admin").password("123").roles("ADMIN");

//        auth.jdbcAuthentication().dataSource();
    }


    @Override
    public void configure(WebSecurity web) throws Exception {
        //super.configure(web);
        //允许对于网站静态资源的无授权访问
        web.ignoring().mvcMatchers(HttpMethod.GET,
                "/",
                "/*.html",
                "/**/*.ico",
                "/**/*.html",
                "/**/*.css",
                "/**/*.js",
                "/**/*.png",
                "/**/*.jpg");
    }

//    // 装载BCrypt密码编码器
//    @Bean
//    public PasswordEncoder passwordEncoder() {
//        return new BCryptPasswordEncoder();
//    }


    @Bean
    public static NoOpPasswordEncoder passwordEncoder() {
        return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
    }
}
